# -*- coding: utf-8 -*-
# @Author : zhaochen
# @Date   : 2019/2/13
# @File   : R32_EC_PaidLoanConditionsAndPaidAttentionCheck.py
# @E-mail : zhaochen@bbdservice.com

'''eof
name:申请企业已结清贷款条件外关注校验
code:R32_EC_PaidLoanConditionsAndPaidAttentionCheck
tableName:
columnName:
groups:企业征信校验
dependencies:EC_DSJ
type:常用指标
datasourceType:在线指标
description:
eof'''

import sys
import re
import datetime
reload(sys)
sys.setdefaultencoding('utf-8')

null_type_list = ['', ' ', None, 'null', 'Null', 'NULL', '/', '-', '--']

def getTable(table_Name):
    try:
        data = EC_DSJ['data'].get(table_Name)
        if data in null_type_list:
            return None
        else:
            # 存在1条为空的数据[{}]
            if isinstance(data, list) and len(data) == 1 and data[0]['ReportNo'] in null_type_list:
                return [{}]
            # 存在空list []
            elif isinstance(data, list) and len(data) == 0:
                return None
            else:
                return data
    except:
        return None


def formatDate(time):
    try:
        import re
        import datetime
        r1 = re.compile(u'年|月|日')
        time = r1.sub('.', time)
        try:
            date = datetime.datetime.strptime(time, '%Y.%m.%d').strftime('%Y%m%d')
        except:
            try:
                date = datetime.datetime.strptime(time, '%Y-%m-%d').strftime('%Y%m%d')
            except:
                try:
                    date = datetime.datetime.strptime(time, '%Y/%m/%d').strftime('%Y%m%d')
                except:
                    try:
                        r = re.compile(r'\D+')
                        tmp = r.sub('', time)
                        if len(tmp) > 8:
                            tmp = tmp[:8]
                        date = datetime.datetime.strptime(tmp, '%Y%m%d').strftime('%Y%m%d')
                    except:
                        date = None
        return date
    except:
        return None

def R32_EC_PaidLoanConditionsAndPaidAttentionCheck():
    ECRPaidBadLoanInfo = getTable('ECRPaidBadLoanInfo')
    if ECRPaidBadLoanInfo is None:
        return u'缺失值'
    else:
        Factor1 = 0
        Factor2 = False
        Factor3 = False
        if ECRPaidBadLoanInfo == [{}]:
            return 1
        else:
            flag = []
            for i in ECRPaidBadLoanInfo:
                try:
                    if i['Fiveclasscode'] == u'关注':
                        if i['Duebillsum'] not in null_type_list:
                            dueBillSum = float(i['Duebillsum'])
                        else:
                            dueBillSum = 0
                        # 修改字段为 还款方式
                        paidtype = i['Repaymethod']
                        settledDate = i['Settlementdate']
                        terminateDate = i['Duebillterminatedate']

                        # Factor1处理 单笔大于1000 则拒绝
                        if dueBillSum > 1000:
                            Factor1 = True


                        # Factor2处理
                        if formatDate(settledDate) is not None and formatDate(terminateDate) is not None:
                            deltaDay = (datetime.datetime.strptime(formatDate(settledDate), '%Y%m%d') - datetime.datetime.strptime(formatDate(terminateDate), '%Y%m%d')).days
                            if deltaDay > 7:
                                Factor2 = True
                        else:
                            pass

                        # Factor3处理
                        if paidtype in [u'政策性还款', u'正常收回']:
                            pass
                        else:
                            Factor3 = True
                except:
                    flag.append(None)
                    continue

            if len(flag) == len(ECRPaidBadLoanInfo):
                return u'缺失值'
            if Factor1 or Factor2 or Factor3:
                return 0
            else:
                return 1


result = R32_EC_PaidLoanConditionsAndPaidAttentionCheck()